from django.test import TestCase
from rest_framework.test import APITestCase
from rest_framework import status
from client.models import UserAccount
from django.core.files.uploadedfile import TemporaryUploadedFile

# Create your tests here.
class ClientTests(APITestCase):
    def setUp(self):
        self.email = "some@qq.com"
        self.password = "qqq111!!!"
        self.re_password = "qqq111!!!"
        self.user = UserAccount.objects.create_user(
            email=self.email, password=self.password
        )

    def test_login(self):
        test_login_data = {"email": self.email, "password": self.password}
        response = self.client.post("/api/token/login/", test_login_data, format="json")
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_signup(self):
        test_signup_data = {
            "email": "2101333733@qq.com",
            "password": "qqq111!!@",
            "re_password": "qqq111!!@",
        }
        response = self.client.post("/api/users/", test_signup_data, format="json")
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

    def test_logout(self):
        test_login_data = {"email": self.email, "password": self.password}
        response = self.client.post("/api/token/login/", test_login_data, format="json")
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        auth_token = response.data["auth_token"]

        # TEST:Logout when it is not authorized
        response = self.client.post("/api/token/logout/")
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
        # TEST:Logout a project when it is authorized
        response = self.client.post(
            "/api/token/logout/", HTTP_AUTHORIZATION="Token " + auth_token
        )
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

    def test_query_profile(self):
        test_login_data = {"email": self.email, "password": self.password}
        response = self.client.post("/api/token/login/", test_login_data, format="json")
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        auth_token = response.data["auth_token"]

        response = self.client.get("/api/profile/")
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
        response = self.client.get(
            "/api/profile/", HTTP_AUTHORIZATION="Token " + auth_token
        )
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertTrue("username" in response.data)
        self.assertTrue("email" in response.data)
        self.assertTrue("phone" in response.data)

    def test_update_profile(self):
        update_data = {"username": "zhangsan", "phone": "123456789"}
        test_login_data = {"email": self.email, "password": self.password}
        response = self.client.post("/api/token/login/", test_login_data, format="json")
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        auth_token = response.data["auth_token"]
        response = self.client.post("/api/update-profile/", update_data, format="json")
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
        response = self.client.post(
            "/api/update-profile/",
            update_data,
            format="json",
            HTTP_AUTHORIZATION="Token " + auth_token,
        )
        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

    def test_query_picture(self):
        response = self.client.get("/api/picture/?id=1")
        self.assertEqual(response.status_code, status.HTTP_200_OK)

